package hc.bre.deci.action

import hc.bre.cmn.RuleScriptBuilder
import hc.bre.cmn.struct.DataStructItem

@groovy.transform.CompileStatic
abstract class ActionBuilder {
	ActionDefinition act
	Object input;
	RuleScriptBuilder builder;

	public void build() {
		if(input == null) {
			builder.apdNl("//empty input for ${act.code}");
			return;
		}

		builder.apdNl("//action of ${act.code}");
		builder.apdNl("${builder.rresult} = ${getActionResult()};");

		DataStructItem resultStruct = getResultStructItem();

		act.resultMap.each { k,v ->
			def rresult = resultStruct.accessRouteOf(k);
			def variable = builder.ruleOfVariable.accessRouteOf(v);
			builder.apdNl("${variable} = ${rresult};");
		};
	}

	public abstract String getActionResult();
	public abstract DataStructItem getResultStructItem();

	public static void build(ActionDefinition actionDef, Object input, RuleScriptBuilder builder) {
		ActionBuilder actionBuilder;
		Class clazz = java.lang.Class.forName(actionDef.getClass().getPackage().getName()+"."+actionDef.type);
		actionBuilder = clazz.getConstructor().newInstance();
		actionBuilder.act = actionDef;
		actionBuilder.input = input;
		actionBuilder.builder = builder;

		actionBuilder.build();
	}
}
